package com.user.service;

import com.data.bean.Account;
import com.data.bean.RegistMassage;
import com.data.util.CheckDataUtils;
import com.data.util.SendMailUtils;
import com.user.dbdao.IndexUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户后台首页
 *
 * @author:HingLo
 * @create 2017-09-22 10:31
 **/
@Service
public class IndexUserService {
    @Autowired
    private IndexUserDao indexUserDao;


    /**
     * @param id    用户id
     * @param state 软件状态
     * @return
     */


    public List<?> findPassSoftwareService(String id, int state) {
        List<?> list = indexUserDao.findPassSoftwareDao(id, state);
        if (!list.isEmpty()) {
            return list;
        }

        return null;
    }

    /**
     * 修改密码
     *
     * @param account
     * @return
     */
    public boolean PasswordChangeService(Account account) {
        return indexUserDao.saveMassageDao(account);

    }

    /**
     * 查找开发者信心
     *
     * @param id
     * @return
     */
    public List<?> lookDeveloperMassageService(String id, int type) {
        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            return indexUserDao.lookDeveloperMassage(id, type);
        }

        return null;
    }

    /**
     * 开发者自己的信息查看
     *
     * @param id
     * @return
     */
    public byte[] developerIconService(String id) {
        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            RegistMassage registMassage = indexUserDao.developerIconDao(id);
            if (registMassage != null) {
                return registMassage.getNumpicture();
            }

        }
        return null;
    }

    public boolean changeEmailService(String email, String id) {
        //检测输入的字符串是否有效，并检测邮箱格式是否正确
        if (CheckDataUtils.stringUtils(email, id) && CheckDataUtils.checkEmail(email)) {
            id = id.trim();
            email = email.trim();
            String code = SendMailUtils.getActive(email);
            if (SendMailUtils.sendChangeEmail(email, code)) {
                return indexUserDao.changeEmailDao(id, code, email);
            }
        }

        return false;
    }

    public boolean activeEmailChangeService(String code, String email) {
        code = code.trim();
        email = email.trim();
        return indexUserDao.activeEmailChangeDao(email, code);


    }
}
